'use strict'

	function socketWebrtc(){
  		let self = this;
  		self.ifLog = true;
  		if (this == window) {throw new Error("create object by new!!!!")};


  		self.start = function(){
  			var wsurl = window.location.protocol+"//"+window.location.hostname+":"+"3000/n2";
  			
			let socket = io(wsurl);
			self.socketIO = socket;
			// 加入房间
			socket.on('connect', function () {
				//socket.emit('join', self.userName);
				var commandMsg = {};
				commandMsg.command = 'init';
				consultationVideoPageControl.handleContorlCommand(commandMsg);
			});
			
			// 监听系统消息
			socket.on('command', function (user, commondMsg,callBack) {
				if(self.ifLog){
					console.log("==================="+user+"======="+socketUserName+"======"+commondMsg.command+"===");
				}

				if(user != socketUserName){
					return;
				}
				if(commondMsg){
					consultationVideoPageControl.handleContorlCommand(commondMsg,callBack);
				}	
			});
			
			socket.on('offer', function (user, Soffer) {
				if(self.ifLog){
					console.log('进入offer');
				}
				let offer = null;
				try{
					offer = JSON.parse(Soffer);
				}catch(e){
					console.log(e);
				}
				
				consultationVideoPageControl.handleRTCSdp(user,Soffer);
			});

			socket.on('answer', function (user, Sanswer) {
				if(self.ifLog){
					console.log('进入answer');
				}
				let answer = null;
				try{
					answer = JSON.parse(Sanswer);
				}catch(e){
					console.log(e);
				}
				
				consultationVideoPageControl.handleRTCSdp(user,Sanswer);
			});

			socket.on('candidate', function (user, Scandidate) {
				let candidate = null;
				try{
					candidate = JSON.parse(Scandidate);
				}catch(e){
					console.log(e);
				}

				if(user == self.userName){
					return false;
				}
				if(self.ifLog){
					console.log("---"+user+"--->"+Scandidate);
				}
			});
			
			socket.on('connect_error', function () {
				if(self.ifLog){
					console.log("connect_error event!");
				}
				socket.close();
				layer.msg("系统错误：服务器链接失败。。。",{/*closeBtn:1,*/shade: [0.8, '#393D49'],time:0,btn: ['退出'],btn1 : function(){backFun();}});
            });
			
			socket.on('disconnect', function () {
				if(self.ifLog){
					console.log("disconnect event!");
				}
			});

		}

		return self;
  	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
    